<template>
  <ve-popup :model-value="visible" @close="close" mode="bottom" :maskClose="true">
    <view class="add-popup">
      <view class="tlt c1 fs36">请选择生成电子合同类型</view>
      <ve-button class="button" size="default" @click="onContractTypeClick('free')">免费电子合同</ve-button>
      <ve-button class="button" size="default" @click="onContractTypeClick('pay')">收费电子合同</ve-button>
    </view>
  </ve-popup>
</template>
<script setup>
  import { ref } from 'vue'
  import { userStore } from '@/stores'
  import { usePermission } from '@/hooks/usePermission'
  const userInfoStore = userStore()

  // 选中的合同类型
  const contractType = ref('')
  // 弹窗是否可见
  const visible = ref(false)
  // 订单信息
  const record = ref(null)
  // 订单类型
  const recordType = ref(null)
  // 显示
  const show = (info, orderType) => {
    record.value = info
    recordType.value = orderType
    // 工单
    if (orderType == 'workOrder') {
      const orderInfo = info.orderInfo
      // 工单对应的订单类型（短租|长租）
      orderType = orderInfo.orderType == 1 ? 'shortOrder' : 'longOrder'
    }
    // 判断是否有付费版的权限
    let hasPayEcontractPermission = false
    // 开通了付费版权限
    userInfoStore.setEpDetail(false).then((res) => {
      const val = res.config
      // 开通了电子合同服务
      if (val.userEcontractEnabled == 1) {
        // 支持的服务类型包含短租
        if (orderType == 'shortOrder' && val.userContractServiceType.includes(0)) {
          hasPayEcontractPermission = true
        }
        if (orderType == 'longOrder' && val.userContractServiceType.includes(1)) {
          hasPayEcontractPermission = true
        }
      }
      // 有付费版合同权限
      if (hasPayEcontractPermission) {
        // 开启弹窗，选择免费版 or 付费版
        visible.value = true
      } else {
        // 跳转到免费版
        onContractTypeClick('free')
      }
    })
  }

  // 关闭
  const close = () => {
    visible.value = false
  }

  // 打开详情
  const onContractTypeClick = (contractType) => {
    // 合同记录-tab 普通版 | 付费版
    const contractIndex = contractType == 'free' ? 0 : 1
    // 存储是否自动触发电子合同生成
    uni.setStorageSync('triggerNewContract', contractIndex)
    visible.value = false
    // 工单
    if (recordType.value == 'workOrder') {
      uni.navigateTo({
        url: `/pages/work/workOrder/detail/index?tab=2&associationorderTab=3&workOrderId=${record.value.id}&orderId=${record.value.refId}&workCode=${record.value.workCode}&orderType=${record.value.orderInfo.orderType}&status=${record.value.status}&contractIndex=${contractIndex}`,
      })
    } else {
      if (recordType.value == 'shortOrder') {
        uni.navigateTo({
          url: `/pages/order/short-order/detail/index?id=${record.value.id}&tab=contract&contractIndex=${contractIndex}`,
        })
      } else {
        uni.navigateTo({
          url: `/pages/order/long-order/detail/index?id=${record.value.id}&tab=contract&contractIndex=${contractIndex}`,
        })
      }
    }
  }

  defineExpose({
    show,
  })
</script>
<style lang="less" scoped>
  .add-popup {
    padding: 0 40rpx 64rpx;
    .tlt {
      margin: 48rpx 0;
      width: 100%;
      text-align: center;
    }
    .button {
      margin-bottom: 32rpx;
    }
  }
</style>
